"""
演示百度翻译案例 --js逆向解析
"""
import time

"""
from: zh
to: en
query: 你好
transtype: translang
simple_means_flag: 3
sign: 232427.485594
token: 85b612d55f683458b86c1c84677a256d
domain: common

from: zh
to: en
query: 明天
transtype: translang
simple_means_flag: 3
sign: 616742.887319
token: 85b612d55f683458b86c1c84677a256d
domain: common

query: 你好 明天
sign: 232427.485594 616742.887319




分析过程：
1.全局搜索，由于携带sign的js文件过多，我们选择simple_means_flag进行搜索
2.打上断点，启动程序，发现sign值疑似由一个b函数执行得到，参数为要被翻译的数据query
3.断点打在函数调用部分，这样可以进入函数看它的执行代码
4.右上角点击向下的箭头(F11)进入函数定义部分
5.函数内部，执行按右上角向右箭头，快捷键为F9
6.复制对应的js代码块，到本地文件
7.报错r没有被定义：r is not defined
    --js代码 >> 使用r 却没有定义r
        --动态数据(相对麻烦)，静态数据(十分好办，不会变化)
    --r是静态数据，不会变化，直接在js文件里面赋值 定义

8.报错n没有被定义：n is not defined
    --js代码使用了n却没有定义
        --根据n的使用，判断n是函数的调用
        --经过调试，找到n函数的定义部分，复制过来
"""




# import requests
# import execjs
# import jsonpath
#
# if __name__ == '__main__':
#     while True:
#         url_ = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'
#
#         headers_ = {
#             "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
#             "Referer": "https://fanyi.baidu.com/?aldtype=16047",
#             "Cookie": "PSTM=1627283384; BIDUPSID=E87874A693DB88716FD0E61C71073BCC; __yjs_duid=1_be57b8bfc8bffacdf8573b0697073d511627307214203; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; BAIDUID=2CE6267F273BA693BF721A0F8EB54795:FG=1; APPGUIDE_10_0_2=1; MCITY=-%3A; BDUSS=GsteHp3TDZ-S1hha2xyOWVhelNifjF2eGhVYUN0a2NxNG5IaHo1Qkx0VGZuTmRqSVFBQUFBJCQAAAAAAAAAAAEAAAC8Ped2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN8PsGPfD7Bjb; BDUSS_BFESS=GsteHp3TDZ-S1hha2xyOWVhelNifjF2eGhVYUN0a2NxNG5IaHo1Qkx0VGZuTmRqSVFBQUFBJCQAAAAAAAAAAAEAAAC8Ped2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN8PsGPfD7Bjb; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=36554_37974_37647_37553_37513_38024_36920_38034_37990_37800_37935_38002_37901_26350_37958_37881; BA_HECTOR=2h018g01ag012h2l01048ibb1hrao071i; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=7; ZFY=dnmTbiILSzEFryTQFoEBw:ADY0cwKOHvWpOJFaXopEB8:C; BAIDUID_BFESS=2CE6267F273BA693BF721A0F8EB54795:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1672669245,1672731635,1672809297,1672835345; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1672835345; ab_sr=1.0.1_NzQ2NTU1ODc0NzMzYTQ2ODllODUwNTQ2ODAyODlhMWQxODQ3OTZjYWVlMjVkZTg4MmEzZDdhZmQwODMyMGYxNzJhN2RkMWU3ODE2NDlkYmQ1NmU5NWYzNDgxN2M3ODI2Yzk2M2Q2ZjI3ZWUwZDZiN2NiOTUxMDVkMTdjOGU5NDQ0NGNjYThmNzk3MjBjNmE3N2VjMzU3ZGVhYmM2ODA0ZjgzNTAyODEzNjY0NDY0NjViYTI4ZjBlMjQ5OWU5OWE4"
#         }
#
#         # 输入要被翻译的数据
#         word = input('请输入你要翻译的内容:')
#
#         # 打开js文件，得到里面的js代码
#         with open('baidu_.js','r') as f:
#             js_data = f.read()
#
#         # 实例化对象
#         js_obj = execjs.compile(js_data)
#
#         # 执行js代码，得到返回数据
#         sign_ = js_obj.call('funa',word)
#         # print(sign_)
#
#         # 构造表单数据    快速添加引号：按住alt不放，在你想要添加引号的地方鼠标左键点一下，点完之后，再输入引号
#         form_data = {
#             "from": "zh",
#             "to": "en",
#             "query": word,
#             "transtype": "translang",
#             "simple_means_flag": "3",
#             "sign": sign_,
#             "token": "85b612d55f683458b86c1c84677a256d",
#             "domain": "common"
#         }
#
#         response_ = requests.post(url_,headers=headers_,data=form_data)
#         py_data = response_.json()
#
#         # 解析
#         res_ = jsonpath.jsonpath(py_data,'$..dst')[0]
#         print(f'翻译结果为：{res_}')
#
#         time.sleep(1)  # 快速导入：alt + enter


import requests
import execjs
import jsonpath

if __name__ == '__main__':
    while True:
        url_ = 'https://fanyi.baidu.com/v2transapi?from=zh&to=en'

        headers_ = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36",
            "Referer": "https://fanyi.baidu.com/?aldtype=16047",
            "Cookie": "PSTM=1627283384; BIDUPSID=E87874A693DB88716FD0E61C71073BCC; __yjs_duid=1_be57b8bfc8bffacdf8573b0697073d511627307214203; REALTIME_TRANS_SWITCH=1; FANYI_WORD_SWITCH=1; HISTORY_SWITCH=1; SOUND_PREFER_SWITCH=1; SOUND_SPD_SWITCH=1; BAIDUID=2CE6267F273BA693BF721A0F8EB54795:FG=1; APPGUIDE_10_0_2=1; MCITY=-%3A; BDUSS=GsteHp3TDZ-S1hha2xyOWVhelNifjF2eGhVYUN0a2NxNG5IaHo1Qkx0VGZuTmRqSVFBQUFBJCQAAAAAAAAAAAEAAAC8Ped2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN8PsGPfD7Bjb; BDUSS_BFESS=GsteHp3TDZ-S1hha2xyOWVhelNifjF2eGhVYUN0a2NxNG5IaHo1Qkx0VGZuTmRqSVFBQUFBJCQAAAAAAAAAAAEAAAC8Ped2AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAN8PsGPfD7Bjb; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=36554_37974_37647_37553_37513_38024_36920_38034_37990_37800_37935_38002_37901_26350_37958_37881; BA_HECTOR=2h018g01ag012h2l01048ibb1hrao071i; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; delPer=0; PSINO=7; ZFY=dnmTbiILSzEFryTQFoEBw:ADY0cwKOHvWpOJFaXopEB8:C; BAIDUID_BFESS=2CE6267F273BA693BF721A0F8EB54795:FG=1; Hm_lvt_64ecd82404c51e03dc91cb9e8c025574=1672669245,1672731635,1672809297,1672835345; Hm_lpvt_64ecd82404c51e03dc91cb9e8c025574=1672835345; ab_sr=1.0.1_NzQ2NTU1ODc0NzMzYTQ2ODllODUwNTQ2ODAyODlhMWQxODQ3OTZjYWVlMjVkZTg4MmEzZDdhZmQwODMyMGYxNzJhN2RkMWU3ODE2NDlkYmQ1NmU5NWYzNDgxN2M3ODI2Yzk2M2Q2ZjI3ZWUwZDZiN2NiOTUxMDVkMTdjOGU5NDQ0NGNjYThmNzk3MjBjNmE3N2VjMzU3ZGVhYmM2ODA0ZjgzNTAyODEzNjY0NDY0NjViYTI4ZjBlMjQ5OWU5OWE4"
        }

        # 输入要被翻译的数据
        word = input('请输入你要翻译的内容:')

        # 打开js文件，得到里面的js代码
        with open('baidu_.js','r') as f:
            js_data = f.read()

        # 实例化对象
        js_obj = execjs.compile(js_data)

        # 执行js代码，得到返回数据
        sign_ = js_obj.call('funa',word)
        # print(sign_)

        # 构造表单数据    快速添加引号：按住alt不放，在你想要添加引号的地方鼠标左键点一下，点完之后，再输入引号
        form_data = {
            "from": "auto",
            "to": "auto",
            "query": word,
            "transtype": "translang",
            "simple_means_flag": "3",
            "sign": sign_,
            "token": "85b612d55f683458b86c1c84677a256d",
            "domain": "common"
        }

        response_ = requests.post(url_,headers=headers_,data=form_data)
        py_data = response_.json()

        # 解析
        res_ = jsonpath.jsonpath(py_data,'$..dst')[0]
        print(f'翻译结果为：{res_}')

        time.sleep(1)  # 快速导入：alt + enter